<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
</head>
<body>
        <div id="app">
            <router-link :to="{name:'home'}">home</router-link>
            <router-link to="/about">关于我们</router-link>
            <router-link to="/login">登陆</router-link>
            <router-link to="/usercenter">账号中心</router-link>
            <router-link to="/usercenter/pwd">密码管理</router-link>
            <router-view></router-view>
        </div>
 
          <script>
         
            const routes = [
                        {
                            name:'home',
                            path:'/',
                            component:{
                                template:'<div> shouye neirong</div>'
                            }
                        },
                        {
                            name:"about",
                            path:'/about',
                            component:{
                                template:'<div>关于我们</div>'
                            }
                        },
                        {
                            path:'/usercenter',
                            component:{
                                template:"<div>个人用户中心<router-view></router-view></div>",
                               
                            } ,
                            children:[
                                    {
                                        path:'pwd',
                                        component:{
                                            template:'<div>密码管理</div>'}
                                    }
                                ]
                        },
                        {
                            path:'/login',
                            component:{
                                template:"<div>登陆</div>"
                            }
                        }



                       ]
            const router=new VueRouter({
                routes
            });
            router.beforeEach((to,from,next)=>{
                var isLogin=false;

                var res=to.matched.some(item=>{
                    return item.path.indexOf("/usercenter")>-1;
                }

                );


                if(res&&!isLogin){
                    next("/login")
                }else{
                    next()
                }
              
            }
            )
            new Vue({
                el:'#app',
                methods:{

                    tiao:function(){

                        router.push({path:'bar'})
                    }
                },
                router
            });
          </script>
</body>
</html>